package com.example.piece.ui.activity

import android.os.Bundle
import android.util.Log
import com.therouter.router.Route
import com.example.base.ui.activity.BaseActivity
import com.example.base.ext.toast
import com.example.base.tools.LauncherIcon.LauncherIconManager
import com.example.piece.R
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.ActivitySocketBinding
import com.example.piece.tools.JWebSocketClient
import com.madchan.library.bean.SwitchIconTask
import java.net.URI
import java.text.SimpleDateFormat

@Route(path = RoutePath.MAIN_SOCKET)
class SocketActivity : BaseActivity<ActivitySocketBinding>() {
    override fun getContentRes(): Int = R.layout.activity_socket
    val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

    override fun initData(savedInstanceState: Bundle?) {
        if (socketClient.isOpen) {
            toast("已经连接上, 无需重复连接!")
            return
        }

        try {
            socketClient.connectBlocking()
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }

        LauncherIconManager.addNewTask(
            SwitchIconTask(
                LoginActivity::class.java.name,
                "$packageName.Login88Activity",
                format.parse("2022-1-29 11:11:11").time,
                format.parse("2022-1-29 17:35:15").time
            ),
            SwitchIconTask(
                LoginActivity::class.java.name,
                "$packageName.Login11Activity",
                format.parse("2022-1-29 17:37:15").time,
                format.parse("2022-1-29 17:40:15").time
            )
        )
    }

    private val socketClient: JWebSocketClient by lazy {
        object : JWebSocketClient(URI.create("ws://ken.free.idcfengye.com/aqsc/websocket")) {

            override fun onMessage(message: String?) {
                super.onMessage(message)
                toast("WebSocket $message")
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e("SocketActivity", "SocketActivityOver")
    }

}